package com.bjsxt.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.UserWallet;
import com.bjsxt.dto.UserWalletDto;
import com.bjsxt.model.Result;
import com.bjsxt.service.UserWalletService;
import com.bjsxt.util.SpringSecurityUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * description:
 * 用户的提币地址控制层
 * 用户的提币地址的权限:
 * 查询: user_wallet_query
 *
 * @author ALIENWARE <br />
 * @date 2021/8/24
 */
@RestController
@RequestMapping("/userWallets")
@Api(tags = "用户的提币地址控制器")
public class UserWalletController {

    /**
     * 用户的提币地址业务层接口
     */
    @Autowired
    private UserWalletService userWalletService;

    /**
     * 分页查询用户的提币地址
     *
     * @param page   分页对象<UserWallet>
     * @param userId 用户 id
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('user_wallet_query')")
    @ApiOperation(value = "分页查询用户的提币地址", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "userId", value = "用户 Id", dataType = "Long", required = true, paramType = "query", example = "1")
    })
    public Result<Page<UserWallet>> findByPage(@ApiIgnore Page<UserWallet> page, @NotNull Long userId) {
        // 查询时间, 我们讲最近新增的, 修改的数据优先展示, 排序 last_update_time
        page.addOrder(OrderItem.desc(UserWallet.COL_LAST_UPDATE_TIME));
        // 调用业务层进行分页数据的查询
        Page<UserWallet> userWalletPage = userWalletService.pageByUserId(page, userId);
        // 返回响应数据
        return Result.success(userWalletPage);
    }

    /**
     * 获取用户的提币地址
     *
     * @param coinId 币种ID
     * @return 统一响应对象
     */
    @GetMapping("/getCoinAddress/{coinId}")
    @ApiOperation(value = "获取用户的提币地址", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinId", value = "币种 ID", dataType = "Long", required = true, paramType = "query", example = "1")
    })
    public Result<List<UserWallet>> getCoinAddress(@PathVariable @NotNull Long coinId) {
        // 获取当前登录的用户 id
        Long userId = SpringSecurityUtils.getCurrentUserId();
        // 调用业务层进行数据的查询
        List<UserWallet> userWallets = userWalletService.getByUserIdAndCoinId(userId, coinId);
        // 进行数据的返回
        return Result.success(userWallets);
    }

    /**
     * 添加用户的提币地址
     *
     * @param userWalletDto 用户的提币地址的参数接受实体类
     * @return 统一响应对象
     */
    @PostMapping
    @ApiOperation(value = "添加用户的提币地址", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户 ID", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "coinId", value = "币种 ID", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "addr", value = "地址", dataType = "Long", required = true, paramType = "query", example = "北京市"),
            @ApiImplicitParam(name = "payPassword", value = "交易密码", dataType = "Long", required = true, paramType = "query", example = "123456")
    })
    public Result<Void> add(@RequestBody @Validated UserWalletDto userWalletDto) {
        // 获取当前用户对象
        Long userId = SpringSecurityUtils.getCurrentUserId();
        // 创建需要保存的对象
        UserWallet userWallet = new UserWallet();
        // 对值进行赋值 copy
        BeanUtil.copyProperties(userWalletDto, userWallet);
        // 设置用户的 id
        userWallet.setUserId(userId);
        // 调用业务层进行数据的保存
        boolean flag = userWalletService.save(userWallet, userWalletDto.getPayPassword());
        // 如果为 true 更新成功
        if (flag) return Result.success("添加成功");
        // 如果为 false 更新失败
        return Result.fail("添加失败");
    }

    /**
     * 删除用户提币地址
     *
     * @param id          用户提币地址 id
     * @param payPassword 交易密码
     * @return 统一响应对象
     */
    @DeleteMapping
    @ApiOperation(value = "删除用户提币地址", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户提币地址 id", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "payPassword", value = "交易密码", dataType = "String", required = true, paramType = "query", example = "123456")
    })
    public Result<Void> delete(@NotNull Long id, @NotBlank String payPassword) {
        // 调用业务层方法进行数据的更新
        boolean flag = userWalletService.removeByIdAndPayPassword(id, payPassword);
        // 如果为 true 删除成功
        if (flag) return Result.success("删除成功");
        // 如果为 false 删除失败
        return Result.fail("删除失败");
    }
}
